#include "flowtestresult.h"
#include "ui_flowtestresult.h"
#include <QMessageBox>
#include "msgbox.h"

FlowTestResult::FlowTestResult(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FlowTestResult)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();

    //Signals and Slots
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &FlowTestResult::retranslateUiSlot);

    initWidget();
}

FlowTestResult::~FlowTestResult()
{
    delete ui;
}

void FlowTestResult::show(const FlowTest &st)
{
    updateFlowTestResultSlot(st);
    QWidget::show();
}

void FlowTestResult::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

void FlowTestResult::on_btnExportFlowRes_clicked()
{
    //todo
    MsgBox::information(this,QString(tr("")),QString(tr("Flow test result exported ok.")));
}

void FlowTestResult::on_btnOK_clicked()
{
    this->hide();
}

void FlowTestResult::on_btnClose_clicked()
{
    this->hide();
}

void FlowTestResult::updateFlowTestResultSlot(const FlowTest &st)
{
    ui->tableWidget->setItem(0,1,new QTableWidgetItem(doubleToString(st.flow.setValue, 2) + "ml/min"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem(doubleToString(st.flow.realValue, 2) + "ml/min"));
    ui->tableWidget->setItem(0,3,new QTableWidgetItem(doubleToString((st.flow.realValue - st.flow.setValue) /st.flow.setValue * 100, 2) + "%"));

    ui->tableWidget->item(0,1)->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget->item(0,2)->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget->item(0,3)->setTextAlignment(Qt::AlignCenter);
    if (windowWidth >= 1280) {
        ui->tableWidget->setRowHeight(0,65);
        ui->tableWidget->horizontalHeader()->setFixedHeight(65);
    } else {
        ui->tableWidget->setRowHeight(0,40);
        ui->tableWidget->horizontalHeader()->setFixedHeight(40);
    }

//    if(st.speedDev != 0)
//    {
//        ui->tableWidget->item(0, 3)->setTextColor(QColor(203,0,0,255));
//    }
}

void FlowTestResult::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void FlowTestResult::initWidget()
{
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->widget->setGraphicsEffect(shadow);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(windowFlags() | Qt::SubWindow);
    ui->tableWidget->setProperty("noinput",true);//不需要虚拟键盘
    ui->tableWidget->verticalHeader()->hide();
    ui->btnExportFlowRes->hide();
    int rowCount = 1;
    ui->tableWidget->setRowCount(rowCount);
    ui->tableWidget->setItem(0,0,new QTableWidgetItem(tr("Flow Rate")));
    int columnWidth1 = 180;
    int columnWidth2 = 250;
    if (windowWidth >= 1280) {
        columnWidth1 = 175;
        columnWidth2 = 185;
        this->setProperty("cssSize", "large");
        this->setFixedWidth(1280);
        this->setFixedHeight(800);
    }
    ui->tableWidget->setColumnWidth(0,columnWidth1);
    ui->tableWidget->setColumnWidth(1,columnWidth2);
    ui->tableWidget->setColumnWidth(2,columnWidth2);
    ui->tableWidget->setColumnWidth(3,columnWidth2);
    ui->tableWidget->item(0,0)->setTextAlignment(Qt::AlignCenter);
}
